package adapter;

import java.util.ArrayList;
import java.util.List;
import object.Dialogue;
import voice.main.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConversationArrayAdapter extends ArrayAdapter<Dialogue> {
	
	private List<Dialogue> dialogues = new ArrayList<Dialogue>();
	
	public ConversationArrayAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
	}
	
	@Override
	public void add(Dialogue object) {
		dialogues.add(object);
		super.add(object);
	}
	
	public int getCount() {
		return this.dialogues.size();
	}

	public Dialogue getItem(int index) {
		return this.dialogues.get(index);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.list_item_conversation, parent, false);
		}

		LinearLayout wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

		TextView dialouge = (TextView) row.findViewById(R.id.dialogue);

		dialouge.setText(getItem(position).content);

		if (getItem(position).left) {
			dialouge.setBackgroundResource(R.drawable.bubble_yellow);
			dialouge.setTypeface(null, Typeface.ITALIC);	
			wrapper.setGravity(Gravity.LEFT);
		}
		else {
			dialouge.setBackgroundResource(R.drawable.bubble_green);
			dialouge.setTypeface(null, Typeface.NORMAL);	
			wrapper.setGravity(Gravity.RIGHT);
		}

		return row;
	}
}
